//
#include "calcular.h"
#include "ui_calcular.h"

Calcular::Calcular(QWidget *parent, AHP * ahp, float ** distancias) :
    QDialog(parent),
    ui(new Ui::Calcular)
{
    ui->setupUi(this);
    this->ahp=ahp;
    this->distancias=distancias;
    this->resolvedor=new Resolvedor();

    list<string> alternativas=ahp->getAlternativas();
    for(list<string>::iterator it=alternativas.begin();it!=alternativas.end();it++){
        ui->comboBox->addItem(it->c_str());
    }
}

Calcular::~Calcular()
{
    delete ui;
}

void Calcular::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void Calcular::on_pushButton_2_clicked()
{
    this->close();
}

void Calcular::on_pushButton_clicked()
{
    list<string> altern = this->ahp->getAlternativas();

    informe=" ";
    float * prioridades=new float[this->ahp->getAlternativas().size()];
    map<string,float> mapPrioridades=this->ahp->calcular();
    informe+="Lista de prioridades:\n";
    for(map<string,float>::iterator it=mapPrioridades.begin();it!=mapPrioridades.end();it++){
        list<string>::iterator it2=altern.begin();
        for(int k=0;k<altern.size();k++){
            if(it->first==*it2){
                prioridades[k]=it->second;
                informe+="    ";
                informe+=it->first;
                informe+=": ";
                informe+=QString::number(prioridades[k]).toStdString();
               // cout << "La prioridad de " << it->second << " es : " << prioridades[k] << endl;
                informe+=".\n";
            }
            it2++;
        }
    }

    list<int> resultado=this->resolvedor->resolverProgDinamica(this->distancias,prioridades,ui->comboBox->currentIndex(),this->ahp->getAlternativas().size());



    string *nombres=new string[altern.size()];

    int i=0;

    for(list<string>::iterator it=altern.begin();it!=altern.end();it++){
        nombres[i]=*it;
        i++;
    }

    informe+="El camino es: ";

    for(list<int>::iterator it=resultado.begin();it!=resultado.end();it++){
        informe+=nombres[*it]+"->";
    }
    informe+=nombres[*resultado.begin()];


    ui->textEdit->setText(QString(informe.c_str()));
}

void Calcular::on_pushButton_3_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this,"Guardar","","*.txt");
    cout << fileName.toStdString() << endl;
    ofstream file(fileName.toAscii());
    file << informe;
    file.close();
}
